package tech.yixiyun.framework.kuafu.kits;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Calendar;
import java.util.Date;

public class DateKit extends DateUtil {

    public static final String DATE_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_YMDHM = "yyyy-MM-dd HH:mm";
    public static final String DATE_YMD = "yyyy-MM-dd";
    public static final String DATE_MDHM = "MM-dd HH:mm";
    public static final String DATE_MDHMS = "MM-dd HH:mm:ss";
    public static final String DATE_HMS = "HH:mm:ss";
    public static final String DATE_MD = "MM-dd";
    public static final String DATE_HM = "HH:mm";


    /**
     * 字符串转Date，支持以下格式的转换：<br/>
     * 时间戳 <br/>
     * yyyy-MM-dd HH:mm:ss<br/>
     * yyyy-MM-dd<br/>
     * MM-dd<br/>
     * HH:mm:ss<br/>
     * HH:mm<br/>
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date) {
        if (StringKit.isBlank(date)) {
            return null;
        }
        date = date.replace("T", " ");
        switch (date.length()) {
            case 24: //2021-03-31 14:51:37.393Z
                date = date.substring(0, 19);
            case 19: //2020-09-09 11:00:00
                return DateKit.parseToDate(date, DATE_YMDHMS);
            case 16: //2020-09-09 11:00
                return DateKit.parseToDate(date, DATE_YMDHM);
            case 14: //09-09 10:10:10
                return DateKit.parseToDate(date, DATE_MDHMS);
            case 13: //时间戳
                return new Date(Long.valueOf(date));
            case 11: //09-09 10:10
                return DateKit.parseToDate(date, DATE_MDHM);
            case 10: //2020-09-09
                return DateKit.parseToDate(date, DATE_YMD);
            case 8: //00:00:00
                return DateKit.parseToDate(date, DATE_HMS);
            case 5: //01-01或者 00：00
                if (date.indexOf("-")>0) {
                    return DateKit.parseToDate(date, DATE_MD);
                } else if (date.indexOf(":") > 0) {
                    return DateKit.parseToDate(date, DATE_HM);
                }

            default:
                throw new RuntimeException("无法识别"+date+"，无法转换为Date类型");
        }
    }


    /**
     * 将特定格式的日期转换为Date对象
     *
     * @param dateStr 特定格式的日期
     * @param format  格式，例如yyyy-MM-dd
     * @return 日期对象
     */
    public static Date parseToDate(CharSequence dateStr, String format) {
        return new DateTime(dateStr, format).toJdkDate();
    }

    /**
     * 比较两个时间相差的天数，不满一天的不算
     * @param a
     * @param b
     * @return
     */
    public static int daysBetween(Date a, Date b) {
        Long time=(a.getTime() - b.getTime()) / (1000*3600*24);
        return Math.abs(time.intValue());
    }


    /**
     * 比较两个时间相差的天数，不满一天的不算
     * @param a 第一个日期
     * @param b 第二个日期
     * @param mode 比较模式，ROUND:四舍五入、CEIL:不满一天按一天算、FLOOR:不满一天不算
     * @return
     */
    public static int daysBetween(Date a, Date b, Mode mode) {
        double time= 1.0 * (a.getTime() - b.getTime()) / (1000*3600*24);
        time = Math.abs(time);
        switch (mode) {
            case FLOOR:
                return Double.valueOf(Math.floor(time)).intValue();
            case CEIL:
                return Double.valueOf(Math.ceil(time)).intValue();
            case ROUND:
                return Double.valueOf(Math.round(time)).intValue();
        }
        return 0;
    }

    /**
     * 计算天数相差的模式
     */
    public static enum Mode {
        /**
         * 四舍五入
         */
        ROUND,
        /**
         * 不满一天的按一天算
         */
        CEIL,

        /**
         * 不满一天的不算
         */
        FLOOR
    }



    /**
     * 得到几天后的时间
     * @param date 起始日期
     * @param day
     * @return
     */
    public static Date getDaysAfter(Date date,int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }
    /**
     * 得到现在起几天后的时间
     * @param day
     * @return
     */
    public static Date getDaysAfter(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }




    /**
     * 得到从今天起几天后的时间
     * @param day
     * @return
     */
    public static Date getDaysAgo(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -day);
        return calendar.getTime();
    }
    /**
     * 得到几天后的时间
     * @param date 起始日期
     * @param day
     * @return
     */
    public static Date getDaysAgo(Date date,int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -day);
        return calendar.getTime();
    }
    /**
     * 获得现在起几分钟前的时间
     * @param minute
     * @return
     */
    public static Date getMinutesAgo(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -minute);
        return calendar.getTime();
    }
    /**
     * 获得现在起几分钟后的时间
     * @param minute
     * @return
     */
    public static Date getMinutesAfter(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * 获得现在起几小时前的时间
     * @param hour
     * @return
     */
    public static Date getHourAgo(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, -hour);
        return calendar.getTime();
    }
    /**
     * 获得现在起几小时后的时间
     * @param hour
     * @return
     */
    public static Date getHourAfter(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }

    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.setTime(date);
        return cal;
    }
    /**
     * 设置之间为0时0分0秒
     * @param date
     * @return
     */
    public static Date HHmmssTo000000(Date date) {
        Calendar cal = getCalendar(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    /**
     * 设置之间为0时0分0秒
     * @param date
     * @return
     */
    public static String HHmmssTo000000Str(Date date) {
        Calendar cal = getCalendar(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return format(cal.getTime(), DATE_YMDHMS);
    }

    /**
     * 设置之间为23时59分59秒
     * @param date
     * @return
     */
    public static Date HHmmssTo235959(Date date) {
        Calendar cal = getCalendar(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }
    /**
     * 设置之间为23时59分59秒
     * @param date
     * @return
     */
    public static String HHmmssTo235959Str(Date date) {
        Calendar cal = getCalendar(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return format(cal.getTime(), DATE_YMDHMS);
    }
}
